package tt.com.serviceprojectmanagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tt.com.common.enums.Status;
import tt.com.common.model.YxCommonServeProviders;
import tt.com.common.util.CommonResult;
import tt.com.serviceprojectmanagement.mapper.YxCommonServeProvidersMapper;
import tt.com.serviceprojectmanagement.service.ServeProvidersService;

import java.util.Date;
import java.util.List;

/**
 * @author BEAN
 * @version 2020
 * @date 2022/8/31 23:59
 */
@Service
public class ServeProvidersServiceImpl extends ServiceImpl<YxCommonServeProvidersMapper, YxCommonServeProviders> implements ServeProvidersService {

    @Autowired
    private YxCommonServeProvidersMapper providersMapper;  //服务项目

    @Override
    public CommonResult delServeProvider(String id, String delUserId) {
        // 判断当前分类是否已经删除
        YxCommonServeProviders yxCommonServeProviders = providersMapper.selectById(id);

        if (yxCommonServeProviders == null || yxCommonServeProviders.getDataStatus() == 0){
            return CommonResult.buildR(Status.IS_NULL);
        }

        Integer result = providersMapper.searchIsExistBySeveProId(yxCommonServeProviders.getProvidersId());
        if (result != null ){
            return CommonResult.buildR(Status.OK, "仓库采购，是否删除？");
        }

        yxCommonServeProviders.setDeleteUserId(delUserId);
        yxCommonServeProviders.setDataStatus(0);
        yxCommonServeProviders.setDeleteTime(new Date());
        return CommonResult.buildR(Status.OK, "删除成功");
    }

    @Override
    public List<YxCommonServeProviders> searchAllProviders(String serveId) {
        List<YxCommonServeProviders> yxCommonServeProviders = providersMapper.selectList(null);
        return yxCommonServeProviders;
    }

    @Override
    public Boolean updateProviders(YxCommonServeProviders providers) {
        YxCommonServeProviders yxCommonServeProviders = providersMapper.selectById(providers.getId());
        if (yxCommonServeProviders.getDataStatus() == 1){
            // 该条数据已删除 不可修改
            return false;
        }
        return providersMapper.updateById(providers) > 0 ;
    }


    @Override
    public Integer insertNewType(YxCommonServeProviders providers) {
        // 判断是否存在
        YxCommonServeProviders serveProviders = providersMapper.selectById(providers.getId());
        if (serveProviders != null){
            // 已存在
            return -1;
        }

        return providersMapper.insert(providers);
    }

    @Override
    public Integer insertNewServeProviders(YxCommonServeProviders serveProviders) {
        return null;
    }
}
